package com.itheima.health.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.health.constant.MessageConstant;
import com.itheima.health.dao.OrderSettingDao;
import com.itheima.health.pojo.OrderSetting;
import com.itheima.health.service.OrderSettingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * 预约设置服务提供者
 */
@Service
@Transactional
public class OrderSettingServiceImpl implements OrderSettingService {

    @Autowired
    OrderSettingDao orderSettingDao;

    /**
     * 添加Excel数据到OrderSetting表
     *
     * @param orderSettings
     */
    @Override
    public void add(List<OrderSetting> orderSettings) {
        if (orderSettings != null && orderSettings.size() > 0) {
            for (OrderSetting orderSetting : orderSettings) {
                int count = orderSettingDao.findByOrderDate(orderSetting.getOrderDate());
                if (count > 0) {
                    //如果该日期在数据库中存在，则更新数据
                    orderSettingDao.update(orderSetting);
                } else {
                    //如果该日期在数据库中不存在，则添加数据
                    orderSettingDao.add(orderSetting);
                }
            }
        }
    }

    /**
     * 日历展示预约设置信息
     */
    @Override
    public List<Map<String, Integer>> getOrderSettingByMonth(String date) {
        //获取当月的预约设置信息
        List<OrderSetting> orderSettingList = orderSettingDao.getOrderSettingByMonth(date);
        //将OrderSettingList转换为map集合
        if (orderSettingList != null && orderSettingList.size() > 0) {
            List<Map<String, Integer>> list = new ArrayList<>();
            for (OrderSetting orderSetting : orderSettingList) {
                Map<String, Integer> map = new HashMap<>();
                map.put("date", orderSetting.getOrderDate().getDate());
                map.put("number", orderSetting.getNumber());
                map.put("reservations", orderSetting.getReservations());
                list.add(map);
            }
            return list;
        }
        return null;
    }

    /**
     * 日历实现预约设置
     *
     * @param orderSetting
     */
    @Override
    public void editNumber(OrderSetting orderSetting) {
        //查询指定日期是否存在
        int count = orderSettingDao.findByOrderDate(orderSetting.getOrderDate());
        if (count>0){
            //存在则更新
            //根据日期查询可预约人数
            int reservations = orderSettingDao.findReservationsByOrderDate(orderSetting.getOrderDate());
            //如果预约人数大于修改的人数，则抛异常
            if (reservations > orderSetting.getNumber()) {
                throw new RuntimeException(MessageConstant.ERROR_ORDERSETTING_EDIT);
            }
            orderSettingDao.update(orderSetting);
        } else {
            //如果表中不存在该日期，则添加
            orderSettingDao.add(orderSetting);
        }


    }

    /**
     * 查询预约日期是否存在
     * @param orderDate
     * @return
     */
    @Override
    public Boolean findByOrderDate(Date orderDate) {
        int count = orderSettingDao.findByOrderDate(orderDate);
        return count > 0;
    }

    /**
     * 查询预约日期是否已约满
     * @param orderDate
     * @return
     */
    @Override
    public Boolean lessNumber2Reservations(String orderDate) {
        int reservations = orderSettingDao.findReservationsByOrderDate2String(orderDate);
        int number = orderSettingDao.findNumberByOrderDate(orderDate);
        return number - reservations <= 0;
    }

    /**
     * 更新t_ordersetting表的已预约人数+1
     */
    @Override
    public void updateReservations(String orderDate) {
        orderSettingDao.updateReservations(orderDate);

    }

    /**
     * 查询预约日期是否存在
     * @param orderDate
     * @return
     */
    @Override
    public Boolean QueryByOrderDate(String orderDate) {
        int count = orderSettingDao.QueryByOrderDate(orderDate);
        return count > 0;
    }
}
